package cn.com.egova.materialdesignexample.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.com.egova.materialdesignexample.R;

/**
 * Created by yuanchao on 2017/4/8.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> mDataSet;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public MyAdapter(List<String> data){
        this.mDataSet = data;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setmOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,parent,false);
//        View view = View.inflate(parent.getContext(),R.layout.item_recyclerview,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv.setText(mDataSet.get(position));

        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(v,position);
                }
            });
        }

        if(mOnItemLongClickListener!=null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(v,position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    //新增数据
    public void addData(int position){
        mDataSet.add(position,"Add");
        notifyItemInserted(position);
    }

    //移除数据
    public void removeData(int position) {
        mDataSet.remove(position);
        notifyItemRemoved(position);
    }

    //更改某个位置的数据
    public void changeData(int position){
        mDataSet.set(position,"Change");
        notifyItemChanged(position);
    }


    /**
     * 注意ViewHolder要继承RecyclerView.ViewHolder.
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        public ViewHolder(View view){
            super(view);
            this.tv = (TextView) view.findViewById(R.id.tv_num);
        }
    }


    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public interface OnItemLongClickListener{
        void onItemLongClick(View view,int position);
    }
}
